The complete program follows:
printRange()
It is OK to use the same parameter names (like x) and
the same local variable names (like index) in several methods.
The scope of parameters and local variables is limited to
the method in which they are declared.
import java.io.*;
class ArrayOps
{
void print( int[] x )
{
for ( int index=0; index < x.length; index++ )
System.out.print( x[index] + " " );
System.out.println();
}
// print elements start through end
void printRange ( int[] x, int start, int end )
{
for ( int index=start; index <= end ; index++ )
System.out.print( x[index] + " " );
System.out.println();
}
}
class ArrayDemo
{
public static void main ( String[] args )
{
ArrayOps operate = new ArrayOps();
int[] ar1 = { -20, 19, 1, 5, -1, 27, 19, 5 } ;
// print elements 1, 2, 3, 4, 5
operate.printRange( ar1, 1, 5 );
}
}
When printRange() is called,
the three actual values given in the call are copied to the parameters
of printRange().
The parameter x refers to the array,
start gets the value "1", end gets the value "5".